package com.taotao.web.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.taotao.common.service.HttpClientService;
import com.taotao.common.service.RedisService;
import com.taotao.manage.pojo.ItemDesc;

@Service
public class ItemDescService {
	
	@Autowired
	private HttpClientService httpClientService;
	
	@Autowired
	private RedisService redisService;
	
	@Value("${MANAGE_TAOTAO_URL}")
	private String MANAGE_TAOTAO_URL;
	@Value("${TAOTAO_ITEMDESC}")
	private String TAOTAO_ITEMDESC;
	
	@Value("${TAOTAO_ITEMDESC_CACHE_KEY}")
	private String TAOTAO_ITEMDESC_CACHE_KEY;
	
	@Value("${TAOTAO_ITEM_CACHE_SECONDS}")
	private Long TAOTAO_ITEM_CACHE_SECONDS;

	public ItemDesc showItemDesc(Long id) {
		String cacheKey = TAOTAO_ITEMDESC_CACHE_KEY+id;
		//先从缓存读取数据
		try {
			ItemDesc itemDesc = redisService.get(cacheKey, new TypeReference<ItemDesc>() {
			});
			if (itemDesc != null) {
				return itemDesc;
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		ItemDesc itemDesc = httpClientService.doGet(MANAGE_TAOTAO_URL+TAOTAO_ITEMDESC+id, new TypeReference<ItemDesc>() {
		});
		
		//将查询到的数据存入缓存
		try {
			redisService.set(cacheKey, itemDesc, TAOTAO_ITEM_CACHE_SECONDS);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		
		return itemDesc;
	}
}
